﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.UserManage
{
    public partial class ManageAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DropDownListHelper.InitDropList<tbDepartmentEntity>(new tbDepartmentEntity().GetAll(), ddlDepartment, "ID", "DepartmentName");//绑定部门

                //编辑
                if (Request.QueryString["id"] != null)
                {
                    string strId = Request.QueryString["id"].ToString();
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        //txtUserName.Enabled = false;//编辑时不可以修改用户名了
                        dataBind(id);
                    }
                }


            }
        }

        private void dataBind(int id)
        {
            Entity.tbUserEntity entity = new ITOMMS.Entity.tbUserEntity();
            entity = entity.GetById(id);
            if (entity != null)
            {
                ViewState["Entity"] = entity;
                ddlDepartment.SelectedValue = entity.DepartmentID.ToString();
                txtEmail.Text = entity.Email;
                txtjob.Text = entity.job;
                txtMobile.Text = entity.Mobile;
                txtMsn.Text = entity.MSN;
                txtPWD.Text = entity.PWD;
                txtQQ.Text = entity.QQ;
                txtRealName.Text = entity.RealName;
                txtRemark.Text = entity.Remark;
                ddlSex.Items.FindByText(entity.sex);
                txtTel.Text = entity.Tel;
                txtUserName.Text = entity.UserName;
                ddlUserType.SelectedValue = entity.RoleGroup.ToString();
                txtWorkNo.Text = entity.WorkNo;
            }
        }


        /// <summary>
        /// 检测是否存在相同用户名,新增时使用
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private bool checkUserSameName(string userName)
        {
            Entity.tbUserEntity entity = new ITOMMS.Entity.tbUserEntity();
            if (entity.GetAllByQuery("UserName='" + userName + "'").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }





        /// <summary>
        /// 检测是否存在相同用户名,更新时使用
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private bool checkUserSameName(string userName, int ID)
        {
            Entity.tbUserEntity entity = new ITOMMS.Entity.tbUserEntity();
            if (entity.GetAllByQuery("UserName='" + userName + "' and id!=" + ID).Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                Entity.tbUserEntity entity = new ITOMMS.Entity.tbUserEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbUserEntity;
                }
                else
                {
                    entity.LoginNum = 0;
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }


                entity.DepartmentID = int.Parse(ddlDepartment.SelectedValue.ToString());
                entity.Email = txtEmail.Text.Trim();
                entity.job = txtjob.Text.Trim();
                entity.Mobile = txtMobile.Text.Trim();
                entity.MSN = txtMsn.Text.Trim();
                entity.PWD = txtPWD.Text.Trim();
                entity.QQ = txtQQ.Text.Trim();
                entity.RealName = txtRealName.Text.Trim();
                entity.Remark = txtRemark.Text.Trim();
                entity.sex = ddlSex.SelectedItem.ToString();
                entity.State = (int)PubEnums.DeleteState.Available;
                entity.CompanyName = "上海汪洋网络";
                entity.State = (int)PubEnums.DeleteState.Available;
                entity.Tel = txtTel.Text.Trim();
                entity.UserName = txtUserName.Text.Trim();
                entity.RoleGroup = int.Parse(ddlUserType.SelectedValue.ToString());
                entity.WorkNo = txtWorkNo.Text.Trim();
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;
                if (entity.ID == 0)
                {
                    if (checkUserSameName(txtUserName.Text.Trim()))
                    {
                        MessageboxInfo.MessageBox(this, MessageType.ERROR, "已经存在有相同的用户名");
                        return;
                    }
                    else
                    {
                        entity.Save(entity);
                    }

                }
                else
                {
                    if (checkUserSameName(entity.UserName, entity.ID))
                    {
                        MessageboxInfo.MessageBox(this, MessageType.ERROR, "已经存在有相同的用户名");
                        return;
                    }
                    else
                    {
                        entity.SaveOrUpdate();
                    }
                }

                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageList.aspx", true);
        }
    }
}
